package com.bestcem.xm.user.grpc.client;


import com.bestcem.bp.grpc.v1.base.Code;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.user.grpc.client.dto.WechatworkEnterpriseInfoDTO;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import zhongyan.xm.wechatwork.v1.services.BaseWechatwork;
import zhongyan.xm.wechatwork.v1.services.BaseWechatworkServiceGrpc;

import javax.annotation.Nonnull;
import java.util.Objects;

/**
 * 微信/企业微信相关grpc接口
 *
 * @author Linzhijian <zhijian.lin@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/12 15:50
 */
@Slf4j
@Service(value = "baseWechatworkGrpcService")
public class BaseWechatworkGrpcService {

    @GrpcClient(GrpcClientNames.XM)
    private BaseWechatworkServiceGrpc.BaseWechatworkServiceBlockingStub stub;


    /**
     * 获取wechatwork_enterprise_info
     *
     * @param corpid
     * @param orgCode
     * @return com.bestcem.xm.user.grpc.client.dto.WechatworkEnterpriseInfoDTO
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/05/13 12:02
     */
    public WechatworkEnterpriseInfoDTO getOneEnterpriseinfo(String corpid, String orgCode) {
        WechatworkEnterpriseInfoDTO enterpriseInfoDTO = new WechatworkEnterpriseInfoDTO();
        BaseWechatwork.ListEnterPriseInfoRequest.Builder builder = BaseWechatwork.ListEnterPriseInfoRequest.newBuilder();
        BaseWechatwork.EnterPriseInfo.Builder enterPriseInfo = BaseWechatwork.EnterPriseInfo.newBuilder();
        if (StringUtils.isNotBlank(corpid)) {
            enterPriseInfo.setCorpid(corpid);
        }
        if (StringUtils.isNotBlank(orgCode)) {
            enterPriseInfo.setOrgCode(orgCode);
        }
        builder.setEnterPriseInfo(enterPriseInfo.build());
        BaseWechatwork.EnterPriseInfoSearchResponse response = stub.listEnterPriseInfo(builder.build());
        if (Objects.isNull(response.getEnterPriseInfoList(0))) {
            return null;
        }
        enterpriseInfoDTO.setId(response.getEnterPriseInfoList(0).getId());
        enterpriseInfoDTO.setOrgCode(response.getEnterPriseInfoList(0).getOrgCode());
        enterpriseInfoDTO.setCorpid(response.getEnterPriseInfoList(0).getCorpid());
        return enterpriseInfoDTO;
    }


    /**
     * 更新wechatwork_enterprise_info
     *
     * @param id
     * @param corpid
     * @param orgId
     * @param orgCode
     * @return void
     * @author Linzhijian <zhijian.lin@idiaoyan.com>
     * @date 2022/05/13 12:02
     */
    @Nonnull
    public void updateEnterpriseinfo(String id, String corpid, String orgId, String orgCode) {
        BaseWechatwork.UpdateEnterPriseInfoRequest.Builder builder = BaseWechatwork.UpdateEnterPriseInfoRequest.newBuilder();
        BaseWechatwork.EnterPriseInfo.Builder enterPriseInfo = BaseWechatwork.EnterPriseInfo.newBuilder();
        if (StringUtils.isNotBlank(id)) {
            enterPriseInfo.setId(id);
        }
        if (StringUtils.isNotBlank(corpid)) {
            enterPriseInfo.setCorpid(corpid);
        }
        if (Objects.nonNull(orgId)) {
            enterPriseInfo.setOrgId(orgId);
        }
        if (Objects.nonNull(orgCode)) {
            enterPriseInfo.setOrgCode(orgCode);
        }
        builder.setEnterPriseInfo(enterPriseInfo.build());
        BaseWechatwork.UpdateResponse response = stub.updateEnterPriseInfo(builder.build());
        if (Objects.isNull(response)) {
            log.error("[USER] grpc updateEnterpriseinfo error id = {} corpid ={} ", id, corpid);
        }
        if (Objects.nonNull(response)) {
            Integer status = response.getStatus().getCodeValue();
            if (!status.equals(Code.OK.getNumber())) {
                log.error(String.format("[USER] grpc updateEnterpriseinfo error id = %s corpid =%s ", id, corpid));
            }
        }
    }
}
